package com.systar.activity;

import java.util.Properties;

import org.junit.Assert;
import org.junit.Test;

import com.castsoftware.java.FileExplorerMock.FileMock;
import com.systar.activity.events.IActivityEvent;
import com.systar.activity.events.ParsingActivityEvent;
import com.systar.activity.events.StatusActivityEvent;
import com.systar.activity.monitor.ActivityManager;
import com.systar.activity.parse.ParseEnvironment;

public class ActivityTest extends ParseEnvironment
{
	/**
	 * Run all classes except UI
	 * 
	 * Simulate incoming files using virtual file system
	 * 
	 * @throws Exception
	 *             if any error
	 */
	@Test
	public void testIncommingFile() throws Exception
	{
		// In parent class, the virtual File system have been initialized on "rootPath" virtual folder
		// create a virtual file in the virtual folder
		FileMock logFile = explorerMock.createFile(rootDir, "CAST-MS80.log.txt");
		// then set the virtual file content with resource file in unit test folder
		logFile.addContent(getResourceContent("CAST-MS80.log.txt"));

		// prepare properties for monitor
		Properties props = new Properties();
		props.put("source1_name", "SRC1");
		props.put("source1_location", rootDir.getAbsolutePath()); // configure with virtual folder

		// create monitor
		ActivityManager manager = new ActivityManager(props, true);
		manager.start();

		// wait while existing file is parsed 
		Thread.yield();
		manager.waitForExecutors();

		// collect events
		int nbActivations = 0;
		int nbErrors = 0;
		for (IActivityEvent e : manager.getDisplayEvents())
		{
			String type = e.getType();

			if (StatusActivityEvent.TYPE.equals(type))
			{
				nbActivations++;
			}
			else if (ParsingActivityEvent.TYPE.equals(type))
			{
				ParsingActivityEvent event = (ParsingActivityEvent) e;
				nbErrors += event.getNbErr();
			}
		}
		Assert.assertEquals("Unexpected activation counts", 1, nbActivations); // ACTIVE then UNACTIVE concatenated to UNACTIVE
		Assert.assertEquals("Unexpected error counts", 2, nbErrors); // 2 errors in TXT log

		// now add a 2nd virtual file
		logFile = explorerMock.createFile(rootDir, "LogSession.xsession");
		logFile.addContent(getResourceContent("LogSession.xsession"));

		// wait while existing file is parsed 
		Thread.yield();
		manager.waitForExecutors();

		// collect events
		nbActivations = 0;
		nbErrors = 0;
		for (IActivityEvent e : manager.getDisplayEvents())
		{
			String type = e.getType();

			if (StatusActivityEvent.TYPE.equals(type))
			{
				nbActivations++;
			}
			else if (ParsingActivityEvent.TYPE.equals(type))
			{
				ParsingActivityEvent event = (ParsingActivityEvent) e;
				nbErrors += event.getNbErr();
			}
		}
		Assert.assertEquals("Unexpected activation counts", 1, nbActivations); // ACTIVE then UNACTIVE concatenated to UNACTIVE
		Assert.assertEquals("Unexpected error counts", 3, nbErrors); // 3 errors in XSESSION log

	}

}
